#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>

int main()
{
    //1.创建通信的套接字
    int lfd = socket(AF_INET,SOCK_STREAM,0);
    if (lfd == -1)
    {
        perror("socket");
        exit(0);

    }
    //2.
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(10000);
    //192.168.0.185
    inet_pton(AF_INET,"192.168.0.185",&addr.sin_addr.s_addr);
    int ret = connect(lfd,(struct sockaddr*)&addr,sizeof(addr));
    
    if(ret == -1)
    {
        perror("connect");
        exit(0);
    }
    int number = 0; 


    while (1)
    {
        char buf[1024];

        sprintf(buf,"hello server...%d\n",number);
        write(lfd,buf,strlen(buf)+1);

        memset(buf,0,sizeof(buf));

        int len = read(lfd,buf,sizeof(buf));
        if(len > 0)
        {
            printf("server say:%s\n",buf);
        }
        else if(len == 0)
        {
            printf("server exit...\n");
            break;
        }else{
            perror("read");
            break;
        }
        number ++;
        sleep(1);
    }

    close(lfd);
    return 0;
}